/**
 * Created by jt on 2017/5/6.
 */
import "./index.less";
import "../../../unit/common/js/bootstrap_js/jquery.min.js";
import "../../../unit/common/js/bootstrap_js/select2.min.js";
import "../../../unit/common/js/bootstrap_js/jquery.multi-select.js";
import "../../../unit/common/js/bootstrap_js/jquery.dataTables.js";
import "../../../unit/common/js/bootstrap_js/form-samples.js";
import "../../../unit/common/js/bootstrap_js/table-editable.js";
import "../../../unit/common/js/bootstrap_js/form-components.js";

jQuery(document).ready(function() {
    App.init();
    // FormComponents.init();
    var token = sessionStorage.getItem('token');
    if (token == null || token =='') {
        location.href = HOST_FRT+'/logins/login.html';
    }
    jQuery('.logOut').attr('href','../logins/login.html');

    let lnkPic;
    let picFlag = true;
    //上传文件
    jQuery('.imgUpload').click(function () {
        picFlag = false;
        let formData = new FormData();
        formData.append('file', $('.J_imgFile')[0].files[0]);
        console.log(formData);

        $.ajax({
            url: HOST_ADDR+'/cpmp/file/upload/img',
            type: 'post',
            cache: false,
            data: formData,
            processData: false,
            contentType: false,
            success: function(resp){
                if(resp.success){
                    // alert('上传文件成功');
                    console.log(resp);
                    lnkPic = resp.data;
                }else{
                    alert(resp.msgInf);
                }
                picFlag = true;
            },
            error:function(err){
                console.log(err);
            }

        })
    });

   //保存
    jQuery('.subKeep').click(function () {
        if (picFlag) {
            let lnkNam = jQuery('.lnkNam').val();
            let lnkUrl = jQuery('.lnkUrl').val();

            if (lnkNam == '') {
                alert('请填写合作商名称！');
                return;
            }
            if (lnkUrl == '') {
                alert('请选择链接地址！');
                return;
            }
            if (lnkPic == '') {
                alert('请添加链接图片！');
                return;
            }

            let data = {
                "pubFlg":'0',
                "lnkNam":lnkNam,
                "lnkUrl": lnkUrl,
                "lnkPic": lnkPic
            };

            console.log(data);

            jQuery.ajax({
                url: HOST_ADDR+'/cpmp/lnkInf/add',
                type: 'post',
                data: JSON.stringify(data),
                dataType: 'json',
                contentType:"application/json",
                headers: {
                    'access_token': token,'login_type':'ums'
                },
                success: function(resp){
                    if (!resp.success) {
                        alert(resp.msgCod+' '+resp.msgInf);
                        return;
                    }
                    console.log(resp);
                    // //页面跳转到index
                    alert('保存成功');
                    location.href = 'partner_list.html';

                },
                error:function(err){
                    console.log(err);
                }
            });
        } else {
            alert('图片上传中，请稍后提交！');
        }
     });

    //发布
    jQuery('.sharePro').click(function () {
        if (picFlag) {
            let lnkNam = jQuery('.lnkNam').val();
            let lnkUrl = jQuery('.lnkUrl').val();

            if (lnkNam == '') {
                alert('请填写合作商名称！');
                return;
            }
            if (lnkUrl == '') {
                alert('请选择链接地址！');
                return;
            }
            if (lnkPic == '') {
                alert('请添加链接图片！');
                return;
            }

            let data = {
                "pubFlg":'1',
                "lnkNam":lnkNam,
                "lnkUrl": lnkUrl,
                "lnkPic": lnkPic
            };

            console.log(data);

            jQuery.ajax({
                url: HOST_ADDR+'/cpmp/lnkInf/add',
                type: 'post',
                data: JSON.stringify(data),
                dataType: 'json',
                contentType:"application/json",
                headers: {
                    'access_token': token,'login_type':'ums'
                },
                success: function(resp){
                    if (!resp.success) {
                        alert(resp.msgCod+' '+resp.msgInf);
                        return;
                    }
                    console.log(resp);
                    // //页面跳转到index
                    alert('发布成功');
                    location.href = 'partner_list.html';

                },
                error:function(err){
                    console.log(err);
                }
            });
        } else {
            alert('图片上传中，请稍后提交！');
        }
    });
    
});